import {
  reqDoorList,
  reqConsumerAlarmPage
} from '../../../../request/api.js'
import {
  showMsg
} from '../../../../utils/config.js'
const app = getApp()
let dictMixin = require('../../../../pages/behaviors/dict/dictMixin.js')

Page({
  behaviors: [dictMixin],
  data: {
    navHeight: app.globalData.navHeight + 'px',
    gonzhonghao1: '/statics/images/gaojing/yangan.png',
    alarmList: [],
    alarmPage: 1,
    alarmIsNextPage:true,
  },

  onLoad(options) {
    console.log(options, 'options');
    this.getDict('device_alarm_type');
    this.setData({
      deviceCode: options.deviceCode,
      deviceType:options.deviceType
    })
    this.getConsumerAlarmPage() // 告警列表
  },

  onShow() {

  },
   // 告警列表
   async getConsumerAlarmPage() {
     let res
     if(this.data.deviceType=='DoorMagnetism'){
      //  门磁
       res = await reqDoorList(this.data.alarmPage,10,{
        imei: this.data.deviceCode,
      })
      if (res.code !== 20000) return showMsg(res.message)
      console.log(res);
     }else{
       res = await reqConsumerAlarmPage(this.data.alarmPage,10,{
        userId: app.globalData.userId,
        homeId: app.globalData.familyInfo.id,
        deviceCode:this.data.deviceCode
      })
      if (res.code !== 20000) return showMsg(res.message)
      console.log(res);
     }
    
    let {
      rows
    } = res.data
    let {
      dictData
    } = this.data
    if (rows && rows.length) {
      rows.forEach(item => {
        dictData.forEach(dict => {
          if (item.alarmType == dict.dickValue) {
            item.dickLabel = dict.dickLabel; //告警类型
          }
        })
      });
    }
    if (this.data.alarmPage === 1) {
      this.setData({
        alarmList: rows,
        alarmIsNextPage: true
      })
    } else if (this.data.alarmPage > 1 && (rows && rows.length)) {
      this.setData({
        alarmList: this.data.alarmList.concat(rows),
        alarmIsNextPage: true
      })
    } else if (this.data.alarmPage > 1 && (!rows || !rows.length)) {
      showMsg('已经到底啦~')
      this.setData({
        alarmIsNextPage: false
      })
    }
  },
    // 滚动到底部触发
    alarmLower() {
      if (!this.data.alarmIsNextPage) return
      this.setData({
        alarmPage: this.data.alarmPage + 1,
      })
      this.getConsumerAlarmPage()
    },
    // 下拉刷新
  async alarmRefresh() {
    this.setData({
      alarmPage: 1,
      alarmIsNextPage: true,
      alarmTriggered: true,
    })
    await this.getConsumerAlarmPage()
    this.setData({
      alarmTriggered: false,
    })
  },

})